﻿using System;
using System.Windows.Forms;

namespace Borg.Framework
{
    public class PlugButton : Button, IAttachable
    {
        private PlugControl _ctl;

        public event EventHandler AfterPlug;
        public event EventHandler BeforePlug;

        public void Attach(RegisterEventArgs args)
        {
            _ctl = new PlugControl(this, new PlugAction(args.SlotInfo, args.PlugTypeInfo));
        }

        public void Attach(SlotInfo slot)
        {
            _ctl = new PlugControl(this, new PlugAction(slot));
        }

        public void Attach(SlotInfo slot, QualifyDelegate qualify)
        {
            _ctl = new PlugControl(this, new PlugAction(slot, qualify));
        }

        public void Attach(SlotInfo slot, PlugTypeInfo plugType)
        {
            _ctl = new PlugControl(this, new PlugAction(slot, plugType));
        }

        public void Attach(SlotInfo slot, string param, object value)
        {
            _ctl = new PlugControl(this, new PlugAction(slot, param, value));
        }

        public void Detach()
        {
            _ctl = null;
        }

        protected virtual void OnAfterPlug(EventArgs args)
        {
            if (AfterPlug != null)
                AfterPlug(this, args);
        }

        protected virtual void OnBeforePlug(EventArgs args)
        {
            if (BeforePlug != null)
                BeforePlug(this, args);
        }

        protected override void OnClick(EventArgs e)
        {
            OnBeforePlug(e);
            _ctl.OnClick(e);
            OnAfterPlug(e);
            base.OnClick(e);
        }
    }
}

